<template>
  <div class="product-details" v-if="pageData.id">
    <div class="info-box">
      <swiper
        class="pic"
        :indicator-dots="true"
        :autoplay="true"
        :interval="4000"
        indicator-active-color="#1772ff"
        circular
      >
        <swiper-item v-for="(item, index) in bannerList" :key="index">
          <img :src="item.url" class="img-size-100" />
          <!-- <text class="tips" v-if="pageData.promote_tag">{{
            pageData.promote_tag
          }}</text> -->
        </swiper-item>
      </swiper>
      <!-- <div class="pic">

      </div> -->
      <div class="main">
        <div class="title">
          <text class="tag" v-if="pageData.special_tag">{{
            pageData.special_tag
          }}</text>
          <text>{{ pageData.title }}</text>
        </div>
        <div class="title_vice" v-if="pageData.title_vice != ''">
          <text>{{ pageData.title_vice }}</text>
        </div>
        <div class="details">
          <div class="left">
            <div class="present-price">
              <text>美粉币</text>
              <text class="number">{{ pageData.lowest_price_name }}</text>
            </div>
            <div class="original-price" v-if="pageData.virtual_price">
              ¥{{ pageData.virtual_price }}
            </div>
          </div>
        </div>
        <div class="share-bar" v-if="isShowShareBtn && pageData.max_bank_amount > 0" @click="toDetailLink">
          <div class="rebate">
            <text class="tag">自购最高可返 {{ pageData.max_bank_amount }}</text>
            <text class="bull">&bull;</text>
            <text>分享最高可赚 {{pageData.max_share_amount}}</text>
          </div>
          <div class="right-icon"><text>分享</text><text class="arrow-l-icon"></text></div>
        </div>
        <div class="right">
          <text
            >快递费:{{
              pageData.freight > 0 ? pageData.freight : "免运费"
            }}</text
          >
          <div>
            <text class="item" v-if="pageData.virtual_sales"
              >销量 {{ pageData.virtual_sales }}</text
            >
            <text class="item">库存 {{ pageData.inventory_total }}</text>
          </div>
        </div>
      </div>
    </div>

    <div class="business-box" v-if="pageData.is_show_merchant != 0">
      <div class="title">商家信息</div>
      <a
        class="main"
        hover-class="none"
        :url="'/pages/shanghu/details?id=' + pageData.merchant.id"
      >
        <div class="pic">
          <img :src="pageData.merchant.logo" class="img-size-100" />
        </div>
        <div class="info">
          <div class="name">{{ pageData.merchant.name }}</div>
          <div class="address">
            {{ pageData.merchant.address }}
          </div>
        </div>
      </a>
    </div>

    <div class="details-box">
      <div class="title">详细信息</div>
      <rich-text :nodes="pageData.content | replaceImg"></rich-text>
    </div>

    <div class="footer">
      <div class="main">
        <a
          class="consult-btn"
          open-type="switchTab"
          url="/pages/index/index"
          hover-class="none"
        >
          <text class="home-icon"></text> <text>去首页</text>
        </a>
        <button class="consult-btn" open-type="contact">
          <text class="pic"></text>
          <text>官方客服</text>
        </button>
        <div
          class="buy-btn"
          :class="{
            dis: pageData.active == 0 || pageData.is_sold_out == 1,
          }"
          v-if="pageData.active == 0 || pageData.is_sold_out == 1"
        >
          {{ pageData.active == 0 ? " 已下架" : "已售罄" }}
        </div>
        <div class="buy-btn" @click="showSpecs" v-else>
          购买
        </div>
      </div>
    </div>

    <van-popup
      closeable
      :show="isShowSpecs"
      @close="isShowSpecs = false"
      position="bottom"
      zIndex="99999"
    >
      <div class="specs-box">
        <div class="product-infor">
          <div class="pic">
            <img :src="pageData.cover_picture" class="img-size-100" />
          </div>
          <div class="right">
            <div class="name">{{ pageData.title }}</div>
            <div class="price">
              <text>美粉币</text>
              <text class="number">{{
                porductPic || pageData.show_amount
              }}</text>
            </div>
            <div v-if="selectProduct.inventory_total >= 0">
              库存{{ selectProduct.inventory_total }}
            </div>
          </div>
        </div>
        <div class="specs-infor">
          <div
            v-for="(item, index1) in skuList"
            :key="index1"
            style="margin-bottom: 20rpx;"
          >
            <div class="title">{{ item.title }}</div>
            <div class="list">
              <div
                class="item"
                v-for="(t, index2) in item.list"
                :key="index2"
                :class="{ active: index2 == specsIndex[index1] }"
                @click="onSpecsIndex(index1, index2)"
              >
                {{ t.title }}
              </div>
            </div>
          </div>
        </div>
        <div class="amount-infor">
          <text class="title">购买数量</text>
          <van-stepper
            :value="quality"
            :integer="true"
            :max="orderMaxNumber"
            :min="orderMinNumber"
            @change="onChange"
          />
        </div>
        <div class="btn-box">
          <div @click="onPlaceOrder" class="btn">立即购买</div>
        </div>
      </div>
    </van-popup>
    <div v-if="isShowShareBtn" class="share-link-btn" @click="toDetailLink">
      <div class="close-btn" @click.stop="isShowShareBtn = false"></div>
    </div>
  </div>
</template>

<script>
import Share from "@/common/share";
import checkLogin from "@/common/check-login.js";
var qs = require("qs");
export default {
  components: {},
  data() {
    return {
      quality: 1,
      isShowShareBtn: true,
      pageData: {},
      skuList: [],
      specsIndex: [],
      isShowSpecs: false, //是否显示规格
    };
  },
  watch: {
    selectProduct() {
      this.quality = this.orderMinNumber;
    },
  },
  computed: {
    porductPic() {
      if (!this.selectProduct) return;
      return this.selectProduct.price;
    },
    orderMaxNumber() {
      if (!this.selectProduct) return;
      return this.selectProduct.order_max_number || 99;
    },
    orderMinNumber() {
      if (!this.selectProduct) return 1;
      return this.selectProduct.order_min_number || 1;
    },
    selectProduct() {
      let { sku_list } = this.pageData;
      if (!sku_list) return this.pageData.inventory_total;
      const product = sku_list.find(
        (item) => item.specifications.toString() == this.selectItem.toString()
      );
      return product;
    },
    selectItem() {
      let { product_category_list } = this.pageData;
      if (!product_category_list) return;
      let idList = [];
      this.specsIndex.map((item, index) => {
        if (item < 0) return;
        idList.push(product_category_list[index].list[item].id);
      });
      // 数组元素升序
      idList.sort(function(a, b) {
        return a - b; // 升序排列
      });
      return idList
    },
    checkOrder() {
      let { product_category_list } = this.pageData;
      if (!product_category_list) return;
      return (
        this.specsIndex.length == product_category_list.length &&
        !this.specsIndex.includes(-1)
      );
    },
    bannerList() {
      let { media, cover_picture } = this.pageData;
      return media && media.length > 0 ? media : [{ url: cover_picture }];
    },
  },
  onLoad(options) {
    let productId = options.id || qs.parse(decodeURIComponent(options.scene)).a;
    if (productId && productId != "undefined") {
      this.productDetail(productId);
    }
    Share.getShareCode();
  },
  onHide() {
    this.isShowSpecs = false;
  },
  onShareAppMessage(res) {
    let { id = "", share_title, share_picture, title } = this.pageData;
    let shareTitle = share_title || title;
    return Share.getShareId({
      id,
      from: res.from,
      shareLink: `shop/productDetails/productDetails`,
      shareTitle,
      shareImgUrl: share_picture,
    });
  },
  methods: {
    toDetailLink() {
      let {
        title,
        share_title,
        share_picture,
        logo,
        product_no,
        id,
      } = this.pageData;
      const shareInfo = JSON.stringify({
        title,
        share_title,
        share_picture,
        logo,
        product_no,
        id,
        share_code: Share.shareCode,
      });
      checkLogin(
        "/shop/productShare/productShare?shareInfo=" +
          encodeURIComponent(shareInfo)
      );
    },
    onPlaceOrder() {
      if (!this.checkOrder) {
        uni.showToast({
          title: "请选择规格",
          duration: 2000,
          icon: "none",
        });
        return;
      }
      const {
        title,
        freight,
        cover_picture,
        is_courier,
        product_no,
        source,
        special_tag,
        id,
      } = this.pageData;
      const orderInfo = JSON.stringify({
        id,
        title,
        freight,
        cover_picture,
        is_courier,
        product_no,
        source,
        special_tag,
        product_number: this.quality,
        price: parseFloat((this.porductPic*100).toPrecision(12)),
        product_sku_id: this.selectProduct.id,
        product_specification_title: this.specificationTitle(),
      });
      if (this.selectProduct.inventory_total >= this.quality) {
        checkLogin(
          `/pages/productOrder/productOrder?orderInfo=${encodeURIComponent(
            orderInfo
          )}`
        );
      } else {
        uni.showToast({
          title: "库存不足",
          duration: 2000,
          icon: "none",
        });
      }
    },
    specificationTitle() {
      let { product_category_list } = this.pageData;
      let specTitle = [];
      this.specsIndex.map((item, index) => {
        specTitle.push(
          product_category_list[index].title +
            ":" +
            product_category_list[index].list[item].title
        );
      });
      return specTitle.join(";");
    },
    showSpecs() {
      this.isShowSpecs = true;
    },
    onSpecsIndex(index1, index2) {
      const index = this.specsIndex[index1] == index2 ? -1 : index2;
      this.$set(this.specsIndex, index1, index);
    },
    onChange(e) {
      this.quality = e.detail;
    },
    productDetail(product_id) {
      this.$api.productDetail({ product_id }).then((res) => {
        if (res.code == 200) {
          this.pageData = res.data;
          this.skuList = res.data.product_category_list;
        }
      });
    },
  },
};
</script>

<style lang="scss">
.product-details {
  background: #f4f4f4;
  color: #333;
  font-size: 32rpx;
  overflow: hidden;
  min-height: 100vh;
  .share-link-btn {
    width: 200rpx;
    height: 230rpx;
    position: fixed;
    z-index: 9999;
    right: 0;
    top: 60vh;
    background: url(https://media.wxcwy.com/mini/images/fxhy_img@2x.png) no-repeat;
    background-size: 100%;
    .close-btn {
      position: absolute;
      top: 0;
      right: 0;
      width: 56rpx;
      height: 56rpx;
      background: url(https://media.wxcwy.com/mini/images/close_g@2x.png) no-repeat top left;
      background-size: 50% 50%;
    }
  }
  .img-size-100 {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
  .info-box {
    background: #fff;
    overflow: hidden;
    margin-bottom: 20rpx;
    .pic {
      height: 500rpx;
      position: relative;
      .tips {
        position: absolute;
        left: 30rpx;
        top: 0;
        bottom: 0;
        margin: auto 0;
        height: 34rpx;
        line-height: 34rpx;
        background: #ee4130;
        border-radius: 4rpx;
        padding: 0 20rpx;
        color: #fff;
        font-size: 24rpx;
      }
    }
    .main {
      padding: 15rpx 30rpx 20rpx;
      overflow: hidden;
      .title {
        margin-bottom: 20rpx;
        .tag {
          box-sizing: border-box;
          height: 34rpx;
          line-height: 34rpx;
          border-radius: 6rpx;
          border: 1px solid #ff7900;
          padding: 0 8rpx;
          color: #ff7900;
          font-size: 22rpx;
          margin-right: 4rpx;
        }
      }
      .title_vice {
        font-size: 24rpx;
        padding-bottom: 20rpx;
        color: #999999;
      }
      .details {
        height: 45rpx;
        display: flex;
        align-items: center;
        justify-content: space-between;
        color: #9f9f9f;
        font-size: 26rpx;
        .left {
          display: flex;
          align-items: center;
          .present-price {
            color: #ff7900;
            font-size: 26rpx;
            font-weight: bold;
            .number {
              font-size: 52rpx;
            }
          }
          .original-price {
            margin-left: 10rpx;
            text-decoration: line-through;
          }
        }
      }
      .share-bar {
        display: flex;
        justify-content: space-between;
        margin-top: 20rpx;
        border: 1px solid #f6d1d1;
        background: rgba(246, 209, 209,.6);
        line-height: 52rpx;
        color: #E96457;
        font-size: 26rpx;
        box-sizing: border-box;
        padding: 0 10rpx;
        border-radius: 4rpx;
        .bull{
          margin: 0 14rpx;
        }
        .right-icon{
          display: flex;
          align-items: center;
          .arrow-l-icon{
            width: 24rpx;
            height: 24rpx;
            margin-left: 6rpx;
            background: url(https://media.wxcwy.com/mini/images/share_arrow@2x.png) no-repeat;
            background-size: 100% 100%;
          }
        }
      }
      .right {
        display: flex;
        justify-content: space-between;
        margin-top: 28rpx;
        color: #333;
        font-size: 24rpx;
        .item {
          color: #9f9f9f;
          margin-left: 20rpx;
        }
      }
    }
  }
  .business-box {
    margin-bottom: 20rpx;
    background: #fff;
    padding: 30rpx;
    overflow: hidden;
    .title {
      height: 45rpx;
      line-height: 45rpx;
      margin-bottom: 20rpx;
    }
    .main {
      display: flex;
      height: 95rpx;
      .pic {
        flex: 0 0 95rpx;
        margin-right: 20rpx;
        border-radius: 8rpx;
      }
      .info {
        .name {
          height: 45rpx;
          line-height: 45rpx;
          overflow: hidden;
          margin-bottom: 10rpx;
        }
        .address {
          height: 32rpx;
          line-height: 32rpx;
          color: #999999;
          font-size: 26rpx;
          background: url(https://media.wxcwy.com/mini/images/dw_icon@2x.png) no-repeat left center;
          margin-right: 6rpx;
          background-size: auto 100%;
          padding-left: 40rpx;
        }
      }
    }
  }
  .details-box {
    padding: 30rpx;
    overflow: hidden;
    background: #fff;
    padding-bottom: 100rpx;
    .title {
      line-height: 45rpx;
      margin-bottom: 20rpx;
    }
    .txt {
      font-size: 26rpx;
      line-height: 40rpx;
    }
  }
  .footer {
    height: 108rpx;
    .main {
      position: fixed;
      bottom: 0;
      left: 0;
      width: 100%;
      height: calc(108rpx + constant(safe-area-inset-bottom));
      height: calc(108rpx + env(safe-area-inset-bottom));
      background: #fff;
      box-sizing: border-box;
      border-top: 1px solid #e7e7e7;
      display: flex;
      padding-bottom: constant(safe-area-inset-bottom);
      padding-bottom: env(safe-area-inset-bottom);
      .consult-btn {
        flex: 1;
        text-align: center;
        color: #1772ff;
        font-size: 26rpx;
        height: 100%;
        display: flex;
        align-items: center;
        flex-direction: column;
        justify-content: center;
        padding: 0 10rpx;
        box-sizing: border-box;
        .pic {
          width: 42rpx;
          height: 42rpx;
          background: url(https://media.wxcwy.com/mini/images/kf_icon@2x.png) no-repeat left center;
          background-size: auto 100%;
          margin-bottom: 5rpx;
        }
        .home-icon {
          width: 42rpx;
          height: 42rpx;
          background: url(https://media.wxcwy.com/mini/images/gosy_icon@2x.png) no-repeat left center;
          background-size: auto 100%;
          margin-bottom: 5rpx;
        }
      }
      .buy-btn {
        flex: 0 0 410rpx;
        background: linear-gradient(
          270deg,
          rgba(44, 172, 255, 1) 0%,
          rgba(40, 123, 252, 1) 100%
        );
        color: #fff;
        height: 100%;
        line-height: 108rpx;
        text-align: center;
        &.dis {
          background: #bdbdbd;
        }
      }
    }
  }
  .specs-box {
    padding: 0 30rpx;
    overflow: hidden;
    .product-infor {
      height: 180rpx;
      display: flex;
      padding: 30rpx 0;
      overflow: hidden;
      color: #666;
      font-size: 26rpx;
      line-height: 38rpx;
      border-bottom: 1px solid #e7e7e7;
      .pic {
        flex: 0 0 180rpx;
        margin-right: 20rpx;
      }
      .name {
        height: 50rpx;
        line-height: 50rpx;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        color: #333333;
        font-size: 36rpx;
        font-weight: bold;
        width: 58vw;
      }
      .price {
        margin: 20rpx 0 10rpx;
        .number {
          color: #ee4130;
          font-size: 32rpx;
          margin-left: 10rpx;
        }
      }
    }
    .specs-infor {
      padding: 30rpx 0 40rpx;
      overflow: hidden;
      border-bottom: 1px solid #e7e7e7;
      color: #333333;
      font-size: 28rpx;
      line-height: 40rpx;
      .list {
        overflow: hidden;
        .item {
          height: 54rpx;
          line-height: 54rpx;
          float: left;
          margin: 20rpx 20rpx 0 0;
          border-radius: 4rpx;
          padding: 0 10rpx;
          background: #f4f4f4;
          box-sizing: border-box;
          &.active {
            border: 1px solid #1772ff;
            background: #dbe9ff;
            color: #1772ff;
          }
        }
      }
    }
    .amount-infor {
      padding: 30rpx 0;
      overflow: hidden;
      border-bottom: 1px solid #e7e7e7;
      height: 60rpx;
      display: flex;
      justify-content: space-between;
      align-items: center;
      color: #333333;
      font-size: 28rpx;
    }
    .btn-box {
      margin-top: 30rpx;
      height: calc(120rpx + constant(safe-area-inset-bottom));
      height: calc(120rpx + env(safe-area-inset-bottom));
      .btn {
        position: fixed;
        bottom: constant(safe-area-inset-bottom);
        bottom: env(safe-area-inset-bottom);
        left: 0;
        width: 100%;
        height: 110rpx;
        line-height: 110rpx;
        text-align: center;
        background: linear-gradient(
          270deg,
          rgba(44, 172, 255, 1) 0%,
          rgba(40, 123, 252, 1) 100%
        );
        color: #ffffff;
        font-size: 36rpx;
        font-weight: bold;
      }
    }
  }
}
</style>
